package newpackage;

import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class CriaPesquisa extends BorderPane {

    TableView tableView = new TableView();
    TableView n = CriaTable();
    PacienteDAO dao = new PacienteDAO();

    public TableView CriaTable() {
        TableColumn nomeCol = new TableColumn();
        nomeCol.setText("Nome");
        nomeCol.setCellValueFactory(new PropertyValueFactory<Paciente, String>("nome"));
        TableColumn enderecoCol = new TableColumn();
        enderecoCol.setText("Endereço");
        enderecoCol.setCellValueFactory(new PropertyValueFactory<Paciente, String>("endereco"));
        TableColumn cepCol = new TableColumn();
        cepCol.setText("CEP");
        cepCol.setCellValueFactory(new PropertyValueFactory<Paciente, String>("cep"));
        TableColumn cidadeCol = new TableColumn();
        cidadeCol.setText("Cidade");
        cidadeCol.setCellValueFactory(new PropertyValueFactory<Paciente, String>("cidade"));
        TableColumn telefoneCol = new TableColumn();
        telefoneCol.setText("Telefone");
        telefoneCol.setCellValueFactory(new PropertyValueFactory<Paciente, String>("telefone"));
        TableColumn estadoCol = new TableColumn();
        estadoCol.setText("Estado");
        estadoCol.setCellValueFactory(new PropertyValueFactory<Paciente, String>("estado"));
        tableView.getColumns().addAll(nomeCol, enderecoCol, cepCol, cidadeCol, telefoneCol, estadoCol);
        return tableView;
    }

    void setData(ObservableList<Paciente> data) {
        tableView.setItems(data);
    }

    public BorderPane RetornaBorder() {

        Label lbNome = new Label("Pesquisa Por Nome:");
        Label lbEndereco = new Label("Pesquisa Por Endereço:");
        Label lbEstado = new Label("Pesquisa Por Estado:");

        final TextField tbNome = new TextField();
        final TextField tbEndereco = new TextField();
        final TextField tbEstado = new TextField();

        Button pesquisar = new Button();
        pesquisar.setText("Pesquisar");
        pesquisar.setStyle("-fx-base: blue;");

        pesquisar.setDefaultButton(true);

        pesquisar.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                List<Paciente> pacientes = null;
                if (!tbNome.getText().isEmpty()) {
                    pacientes = dao.findByNome(tbNome.getText());
                } else {
                    if (!tbEndereco.getText().isEmpty()) {
                        pacientes = dao.findByEndereco(tbEndereco.getText());
                    } else {
                        if (!tbEstado.getText().isEmpty()) {
                            pacientes = dao.findByEstado(tbEstado.getText());
                        } else {
                            pacientes = dao.findAll();
                        }
                    }
                }

                setData(FXCollections.observableArrayList(pacientes));
            }
        });

        Button remover = new Button();
        remover.setText("Remover");
        remover.setStyle("-fx-base: blue;");

        remover.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                List<Paciente> pacientes = null;
                Paciente paciente = (Paciente) n.getSelectionModel().getSelectedItem();
                dao.remove(paciente);
                pacientes = dao.findAll();
                setData(FXCollections.observableArrayList(pacientes));
            }
        });

        Button editar = new Button();
        editar.setText("Editar");
        editar.setStyle("-fx-base: blue;");
        editar.setMinWidth(68);

        editar.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                List<Paciente> pacientes = null;
                Paciente paciente = (Paciente) n.getSelectionModel().getSelectedItem();
                pacientes = dao.findAll();
                setData(FXCollections.observableArrayList(pacientes));
                final Stage stage = new Stage();
                javafx.scene.Group rootGroup = new javafx.scene.Group();
                Scene scene = new Scene(rootGroup, 480, 340);
                stage.setScene(scene);
                stage.centerOnScreen();
                stage.show();
                BorderPane edicao = new BorderPane();
                CriaEdicao c = new CriaEdicao();
                edicao.setCenter(c.CriaGrid(paciente));
                rootGroup.getChildren().add(edicao);

            }
        });




        GridPane gp2 = new GridPane();
        gp2.setPadding(new Insets(10, 10, 10, 10));
        gp2.setHgap(10);
        gp2.setVgap(10);
        gp2.add(lbNome, 0, 0);
        gp2.add(tbNome, 0, 1);
        gp2.add(lbEndereco, 0, 3);
        gp2.add(tbEndereco, 0, 4);
        gp2.add(lbEstado, 0, 6);
        gp2.add(tbEstado, 0, 7);
        gp2.add(pesquisar, 0, 8);
        gp2.add(remover, 0, 10);
        gp2.add(editar, 0, 12);



        GridPane.setHalignment(pesquisar, HPos.CENTER);
        GridPane.setHalignment(remover, HPos.CENTER);
        GridPane.setHalignment(editar, HPos.CENTER);

        HBox hb = new HBox();

        hb.getChildren().addAll(gp2, n);
        BorderPane bp = new BorderPane();
        bp.setCenter(hb);
        return bp;
    }
}
